/*
 * COMSAT
 * Copyright (C) 2014, Parallel Universe Software Co. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */
package co.paralleluniverse.fibers.dropwizard;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import static com.codahale.metrics.MetricRegistry.name;
import java.util.concurrent.TimeUnit;
import org.apache.http.config.Registry;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.nio.conn.ManagedNHttpClientConnection;
import org.apache.http.nio.conn.NHttpClientConnectionManager;
import org.apache.http.nio.conn.NHttpConnectionFactory;
import org.apache.http.nio.conn.SchemeIOSessionStrategy;
import org.apache.http.nio.reactor.ConnectingIOReactor;

public class InstrumentedNClientConnManager extends PoolingNHttpClientConnectionManager {

    public InstrumentedNClientConnManager(final ConnectingIOReactor ioreactor, final NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final MetricRegistry metricRegistry, final Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, final long timeToLive, final TimeUnit tunit, final DnsResolver dnsResolver, final String name) {
        super(ioreactor, connFactory, iosessionFactoryRegistry, schemePortResolver, dnsResolver, timeToLive, tunit);
        metricRegistry.register(name(NHttpClientConnectionManager.class, name, "available-connections"),
                new Gauge<Integer>() {
                    @Override
                    public Integer getValue() {
                        // this acquires a lock on the connection pool; remove if contention sucks
                        return getTotalStats().getAvailable();
                    }
                });
        metricRegistry.register(name(NHttpClientConnectionManager.class, name, "leased-connections"),
                new Gauge<Integer>() {
                    @Override
                    public Integer getValue() {
                        // this acquires a lock on the connection pool; remove if contention sucks
                        return getTotalStats().getLeased();
                    }
                });
        metricRegistry.register(name(NHttpClientConnectionManager.class, name, "max-connections"),
                new Gauge<Integer>() {
                    @Override
                    public Integer getValue() {
                        // this acquires a lock on the connection pool; remove if contention sucks
                        return getTotalStats().getMax();
                    }
                });
        metricRegistry.register(name(NHttpClientConnectionManager.class, name, "pending-connections"),
                new Gauge<Integer>() {
                    @Override
                    public Integer getValue() {
                        // this acquires a lock on the connection pool; remove if contention sucks
                        return getTotalStats().getPending();
                    }
                });
    }

}
